<?php

class myUser extends sfMelodyUser
{
	public function initialize(sfEventDispatcher $dispatcher, sfStorage $storage, $options = array())
	{
		$options['timeout'] = false;		
		parent::initialize($dispatcher, $storage, $options);

	}
	public function register()
	{
		//$this->getTracker()->setVar('inscription', array('use_flash' => true,));
	}
	public function signIn($user, $remember = false, $con = null)
	  {
		parent::signIn($user, $remember, $con);
		$q = Doctrine_Query::create()
		->from('adverts n')
		->where('n.id_membre = ?', $user->getId())
		->andWhere('n.date_fin < ?', date("Ymdhis"));
		$q->delete();
		
		$q = Doctrine_Query::create()
		->from('adverts n')
		->leftJoin("n.SfGuardGroup")
		->where('n.id_membre = ?', $user->getId());
		$advert = $q->execute();
		
		if(count($advert) > 0)
		{
			//$this->getTracker()->setVar('connexion_averties', array('use_flash' => true,));
			$collec = new Doctrine_Collection('SfGuardGroup');
			foreach($advert as $adver)
				$collec->add($adver["SfGuardGroup"]);
				
			$user->setGroups($collec);
		}
		//else
			//$this->getTracker()->setVar('connexion', array('use_flash' => true,));
	  }
	
	public function getPlatforms($id,$already_register = true,$browscap = array())
	{
		
		if($this->isAuthenticated() && $already_register)
		{
			
			$q = Doctrine_Query::create()
			->from('clientsplatforms n')
			->where('n.id_client = ?', $id)
			->leftJoin("n.Platform")
			->leftJoin("n.Platformgroup pg");
			return $q->execute();
		}
		else
		{
			
			$q = Doctrine_Query::create()
			->from('Platform n')
			->where('n.name = ?', $browscap->platform)
			->orWhere('n.name = ?', $browscap->browser);
			
			$plat = $q->execute();
			$return = array();
			
			if(count($plat) == 2)
			{
				$version = Doctrine_Core::getTable('clientsplatforms')->findOneByVersion($browscap->majorver);
				$return[] = array("id_platform" => $plat[0]["id"],
									  "version" => $version["idversions"],
									  "id_group_platform" => $plat[0]["type"],
									  "id_client" => "-1",
									  "Platform" => $plat[0]
									  );
				$return[] = array("id_platform" => $plat[1]["id"],
									  "id_group_platform" => $plat[1]["type"],
									  "id_client" => "-1",
									  "Platform" => $plat[1]
									  );
			}
			elseif(count($plat) == 1)
			{
				if($plat[0]["name"] == $browscap->platform)
				{
					$version = Doctrine_Core::getTable('clientsplatforms')->findOneByVersion($browscap->majorver);
					$return[] = array("id_platform" => $plat[0]["id"],
									  "version" => $version["idversions"],
									  "id_group_platform" => $plat[0]["type"],
									  "id_client" => "-1",
									  "Platform" => $plat[0]
									  );
				}
				else
				{
					$return[] = array("id_platform" => $plat[0]["id"],
									  "id_group_platform" => $plat[0]["type"],
									  "id_client" => "-1",
									  "Platform" => $plat[0]
									  );
				}
			}
			
			return $return;
		}
		
	}
	public function updatePoint($point, $mess)
	{
		$user = $this->getProfile();
		$user->setNbPoint($user->getNbPoint() + intval($point));
		$user->save();
		
		$evo = new Point();
		$evo->setIdClient($this->getProfile()->getId());
		if ($cache = sfContext::getInstance()->getViewCacheManager())
			$cache->remove('@sf_cache_partial?module=headers&action=_newmp&sf_cache_key='.$this->getProfile()->getId());
		
		$evo->setPoint(intval($point));
		$evo->setRaison($mess);
		$evo->save();
	}
	public function getBrowscap()
	{
		if(!function_exists ("get_browser_local"))
			include("Browscap.php");
		return get_browser_local();
	}

	
}
